﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SkypeStudio.Framework;
using SkypeStudio.Controls.Services;
using SkypeStudio.Controls.ViewModels;
using SkypeStudio.Controls.Model;
using System.Windows.Threading;
using System.Windows.Navigation;
using System.Windows;
using System.Windows.Input;

namespace PongsathonKeng.SkypeStudio
{
    public interface ISkypeStudioWindowView : IView
    {

    }
    class SkypeStudioWindowViewModel : ViewModelBase<ISkypeStudioWindowView>
    {

        private NavigationService _NavService;
        private object _ChatListView;

        public SkypeStudioWindowViewModel(ISkypeStudioWindowView view)
            : base(view)
        {
            // _SkypeApplicationService = skypeApplicationService;
            // _SkypeApplicationService.SkypeProcessAttachStatusChanged += SkypeServiceSkypeProcessAttachStatusChanged;
        }

        private object _ContactListView;
        public object ContactListView
        {
            get
            {
                return _ContactListView;
            }
            set
            {
                if (_ContactListView == value)
                    return;

                if (_ContactListView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
                    {
                        ContactListViewModel contactListViewModel = ((IView)_ContactListView).DataContext as ContactListViewModel;
                        contactListViewModel.RequestStartChat -= ContactListViewModelRequestStartChat;
                        _NavService = null;
                    });
                }

                _ContactListView = value;


                if (_ContactListView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
                    {
                        ContactListViewModel contactListViewModel = ((IView)_ContactListView).DataContext as ContactListViewModel;
                        contactListViewModel.RequestStartChat += ContactListViewModelRequestStartChat;
                        _NavService = NavigationService.GetNavigationService((DependencyObject)_ContactListView);
                    });
                }
            }
        }

        void ContactListViewModelRequestStartChat(Chat obj)
        {
            if (_NavService == null)
            {
                _NavService = NavigationService.GetNavigationService((DependencyObject)ChatListView);
                _NavService.Navigated += new NavigatedEventHandler(_NavService_Navigated);
            }

            this.IsChatTabSelected = true;
            SendMessageViewModel sendMessageViewModel = ((IView)SendMessageView).DataContext as SendMessageViewModel;
            sendMessageViewModel.ChatSource = obj;
            _NavService.Navigate(SendMessageView);
        }

        public object SkypeTooBarView { get; set; }

        private object _SendMessageView;
        public object SendMessageView
        {
            get
            {
                return _SendMessageView;
            }
            set
            {
                if (_SendMessageView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
                    {
                        SendMessageViewModel sendMessageViewModel = ((IView)_SendMessageView).DataContext as SendMessageViewModel;
                        sendMessageViewModel.RequestBackToChatList -= ContactListViewModelRequestBackToChatList;
                    });
                }

                _SendMessageView = value;

                if (_SendMessageView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
                    {
                        SendMessageViewModel sendMessageViewModel = ((IView)_SendMessageView).DataContext as SendMessageViewModel;
                        sendMessageViewModel.RequestBackToChatList += ContactListViewModelRequestBackToChatList;
                    });
                }
            }
        }

        void ContactListViewModelRequestBackToChatList(Chat obj)
        {
            _NavService.Navigate(ChatListView);
        }

        public object ChatListView
        {
            get
            {
                return _ChatListView;
            }
            set
            {

                if (_ChatListView == value)
                    return;

                if (_ChatListView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
                     {
                         ChatListViewModel chatListViewModel = ((IView)_ChatListView).DataContext as ChatListViewModel;
                         chatListViewModel.RequestOpendChat -= ChatListViewModelRequestOpendChat;
                         _NavService = null;
                     });
                }

                _ChatListView = value;

                if (_ChatListView != null)
                {
                    Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
                     {
                         ChatListViewModel chatListViewModel = ((IView)_ChatListView).DataContext as ChatListViewModel;
                         chatListViewModel.RequestOpendChat += ChatListViewModelRequestOpendChat;
                         _NavService = NavigationService.GetNavigationService((DependencyObject)_ChatListView);
                     });
                }

                NotifyOfPropertyChange(() => ChatListView);
            }
        }

        void ChatListViewModelRequestOpendChat(Chat obj)
        {
            if (_NavService == null)
            {
                _NavService = NavigationService.GetNavigationService((DependencyObject)ChatListView);
                _NavService.Navigated += new NavigatedEventHandler(_NavService_Navigated);
            }

            SendMessageViewModel sendMessageViewModel = ((IView)SendMessageView).DataContext as SendMessageViewModel;
            sendMessageViewModel.ChatSource = obj;
            _NavService.Navigate(SendMessageView);
        }

        void _NavService_Navigated(object sender, NavigationEventArgs e)
        {
            NotifyOfPropertyChange(() => IsChatMessageActive);
        }

        //private ICommand _BackToChatListCommand;
        //public ICommand BackToChatListCommand
        //{
        //    get
        //    {
        //        if (this._BackToChatListCommand == null)
        //        {
        //            _BackToChatListCommand = new RelayCommand(BackToChatList);
        //        }
        //        return _BackToChatListCommand;
        //    }

        //}

        public Visibility IsChatMessageActive
        {
            get
            {
                if (_NavService == null)
                    return Visibility.Collapsed;

                return _NavService.Content == SendMessageView ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        //private void BackToChatList(object param)
        //{
        //    _NavService.Navigate(ChatListView);
        //}

        private bool _IsContactTabSelected = true;
        public bool IsContactTabSelected
        {
            get
            {
                return _IsContactTabSelected;
            }
            set
            {
                if (_IsContactTabSelected == value)
                    return;
                _IsContactTabSelected = value;
                NotifyOfPropertyChange(() => IsContactTabSelected);
            }
        }

        private bool _IsChatTabSelected;
        public bool IsChatTabSelected
        {
            get
            {
                return _IsChatTabSelected;
            }
            set
            {
                if (_IsChatTabSelected == value)
                    return;
                _IsChatTabSelected = value;
                NotifyOfPropertyChange(() => IsChatTabSelected);
            }
        }
    }
}
